
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;


public class GridFrame extends javax.swing.JFrame {

   private JTable table;

    private List data =Collections.synchronizedList(new ArrayList());
    private  List col =Collections.synchronizedList(new ArrayList());
    private final boolean editable;
    private GridModel gridModel;
    private final boolean auto_resize_off;
    private final boolean enable_button_panel;
    private JButton okButton;
    public GridFrame(String title,boolean enable_button_panel,boolean auto_resize_off,boolean editable) {
       
      this.editable =editable;   
      this.auto_resize_off = auto_resize_off;
      this.enable_button_panel=enable_button_panel;
       JFrame.setDefaultLookAndFeelDecorated(true);    
                 try {
            
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
       }
                 
      this.setIconImage(icon);
        this.setTitle(title);
      
         
    }

     public void showGrid() {
          
        initComponents();
         gridModel = new GridModel();
      
        
        table = new JTable();
        gridModel.setCellEditable(this.editable);
        table.setModel(gridModel);

        
      
     
        table.getTableHeader().setReorderingAllowed(false);
       if(this.auto_resize_off){
           table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       }else{
           table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
       }
        
       
        table.setRowHeight(20);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
     
         this.add(scrollPane,BorderLayout.CENTER);
         
         if(this.enable_button_panel) {
           JPanel p  = new JPanel();
           okButton = new JButton("     OK      ");
           
           okButton.addActionListener(new ActionListener() {

                 @Override
                 public void actionPerformed(ActionEvent e) {
                    dispose();
                 }
               
           });
           p.add(okButton);
           add(p,BorderLayout.PAGE_END);
           
       }
         pack();
         this.setLocationRelativeTo(null);
         this.setVisible(true);
    }
     public void showGrid(String title,boolean enable_button_panel,boolean auto_resize_off,boolean editable) {
         GridFrame frame = new GridFrame(title,enable_button_panel,auto_resize_off,editable);
         frame.showGrid();
     }
     public void addRow(String r) {
        data.add(r);
      }
      public void addCol(String c) {
        col.add(c);
        }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    class GridModel extends AbstractTableModel{
        int i;
        private boolean editable;
  

        @Override
        public int getRowCount() {
          try{
                i = data.size() / getColumnCount();
            }catch(Exception e) {

            }


           return i;
        }

        @Override
        public int getColumnCount() {
           return col.size();
        }

        @Override
        public String getColumnName(int i) {
            String c = "";
            if(i <=getColumnCount()) {
                c = (String)col.get(i);
            }
            return c;
        }

        public Class getColClass(int i) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int r,int c) {
            return editable;
        }
        
        public void setCellEditable(boolean editable) {
            this.editable=editable;
        }
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
           return  data.get((rowIndex*getColumnCount())+columnIndex);
        }
    }
    public  java.net.URL imgURL = getClass().getResource("DomainMath.png");
    public   Image icon = Toolkit.getDefaultToolkit().getImage(imgURL);
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
